﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Kitware.VTK;
namespace HAVTKPack
{
    /// <summary>
    /// 用于表示一个geometry节点
    /// </summary>
   public class GeometryNode:Node
   {

       #region 构造函数

       public GeometryNode(string name,Geometry geometry)
           : base(name)
       {
           this._geometry = geometry;

           this._geometry.ParentNode = this;

           //_filters = new List<GeometryFilter>();

         
 
       }


       #endregion




       #region 字段属性



       //protected REPRESENTATIONTYPE _repType=REPRESENTATIONTYPE.WIREFRAME;

       //public REPRESENTATIONTYPE RepType
       //{
       //    get { return _repType; }
       //    set {
       //        if (_repType != value)
       //        {

       //            _repType = value;

       //            this.OnSetRepresentationType();

       //        }
           
       //    }
       //}

        Geometry _geometry;
       /// <summary>
       /// PolyData 数据源
       /// </summary>
        public Geometry Geometry
        {
            get { return _geometry; }
            set { 
                _geometry = value; 
            

            }
        }


       #endregion



        #region 受保护的方法

        protected void RenderGeometry()
       {
 
       }

       protected void RenderFilter()
       {
         
       }

       #endregion



       #region 公有方法


       public virtual void AddFilter(GeometryFilter filter)
       {
           filter.ParentNode = this;
           this._filters.Add(filter);
           if (this.scene!=null)
           {
               if (this.scene.IsRendering)
               {
                   filter.Render();
               }   
           }
       }

       public virtual void RemoveFilter(GeometryFilter filter)
       {
     
           this._filters.Remove(filter);
       }

        public override void Render()
        {
            //注意渲染的先后顺序
            this.Geometry.Render();


            //this.InitProp();


            foreach (GeometryFilter item in this._filters)
            {
                item.Render();
            }

            //this.InitProp();

        }


        public override void StopRender()
        {
            foreach (GeometryFilter item in this._filters)
            {
                item.StopRender();

            }

            _geometry.StopRender();
        }



        public override void InitProp()
        {
            base.InitProp();

            //this.OnSetVisibility();

            //this.OnSetRepresentationType();

            //关于vtkTransForm的转换受顺序影响  本程序暂时按照以下顺序执行  哈哈
            this.OnSetPosition();
            this.OnSetScale();
            this.OnSetRotation();

            if (this.scene != null)
            {
                if (this.scene.IsRendering)
                {
                    this.scene.Update();
                }
            }

        }



       /// <summary>
       /// 返回指定的Filter
       /// </summary>
       /// <param name="filterId"></param>
       /// <returns></returns>
        public GeometryFilter GetFilter(string filterId)
        {
            foreach (GeometryFilter item in this._filters)
            {
                if (item.Id==filterId)
                {
                    return item;
                    
                }
                
            }

            return null;
           
        }

       #endregion




        #region 属性值改变方法


        protected override void OnSetPosition()
        {
            base.OnSetPosition();
            if (this.scene == null)
            {
                return;

            }

            if (this.scene.IsRendering)
            {
                if (this.Geometry !=null )
                {
                    if (this.Geometry.GeometryTransform != null)
                    {
                        this.Geometry.GeometryTransform.Translate(this.Position.X, this.Position.Y, this.Position.Z);
                        
                        //for (int i = 0; i < this._filters .Count ; i++)
                        //{
                        //    if (((GeometryFilter)this.Filters[i]).FilterTransform==null )
                        //    {
                        //        continue;
                        //    }
                        //    ((GeometryFilter)this.Filters[i]).FilterTransform.Translate(this.Position.X, this.Position.Y, this.Position.Z);
                        //}

                       //( (GeometryFilter )this.Filters[0]).FilterTransform.Translate(this.Position.X, this.Position.Y, this.Position.Z);
                    
                    }
                }


                //if (this.Geometry as HydrolicGridGeometry !=null)
                //{

                //    ((HydrolicGridGeometry)this.Geometry).Transform .Translate(this.Position.X, this.Position.Y, this.Position.Z);

                //}
               

                //this.scene.Update();
            }


        }
        protected override void OnSetScale()
        {
            base.OnSetScale();
            if (this.scene == null)
            {
                return;

            }
            if (this.scene.IsRendering)
            {

                if (this.Geometry !=null )
                {
                    if (this.Geometry.GeometryTransform != null)
                    {
                        this.Geometry.GeometryTransform.Identity();
                        this.Geometry.GeometryTransform.Scale(this.Scale.X, this.Scale.Y, this.Scale.Z);


                        for (int i = 0; i < this._filters.Count; i++)
                        {
                            //if (((GeometryFilter)this.Filters[i]).FilterTransform == null)
                            //{
                            //    continue;
                            //}
                            ////((GeometryFilter)this.Filters[i]).FilterTransform.Identity();
                            //((GeometryFilter)this.Filters[i]).FilterTransform.Scale(this.Scale.X, this.Scale.Y, this.Scale.Z);
                        }
                    }
                }

                //if (this.Geometry as HydrolicGridGeometry !=null)
                //{
                //     ((HydrolicGridGeometry)this.Geometry).Transform.Scale(this.Scale.X, this.Scale.Y, this.Scale.Z);
                //}
               
                //this.scene.Update();
            }
        }

        protected override void OnSetRotation()
        {
            base.OnSetRotation();
            if (this.scene == null)
            {
                return;

            }
            if (this.scene.IsRendering)
            {

                if (this.Geometry != null)
                {
                    if (this.Geometry.GeometryTransform != null)
                    {
                        this.Geometry.GeometryTransform.RotateX(this.Rotation .X );
                        this.Geometry.GeometryTransform.RotateY (this.Rotation.Y );
                        this.Geometry.GeometryTransform.RotateZ (this.Rotation.Z );

                        //for (int i = 0; i < this._filters.Count; i++)
                        //{
                        //    if (((GeometryFilter)this.Filters[i]).FilterTransform == null)
                        //    {
                        //        continue;
                        //    }
                        //    ((GeometryFilter)this.Filters[i]).FilterTransform.RotateX(this.Rotation.X);
                        //    ((GeometryFilter)this.Filters[i]).FilterTransform.RotateY (this.Rotation.Y );
                        //    ((GeometryFilter)this.Filters[i]).FilterTransform.RotateZ (this.Rotation.Z );
                        //}

                    }
                }

                //if (this.Geometry as HydrolicGridGeometry != null)
                //{
                    
                //    ((HydrolicGridGeometry)this.Geometry).Transform.RotateX(this.Rotation.X);
                //    ((HydrolicGridGeometry)this.Geometry).Transform.RotateY(this.Rotation.Y);
                //    ((HydrolicGridGeometry)this.Geometry).Transform.RotateZ(this.Rotation.Z);
                
                //}
                

                //this.scene.Update();
            }
        }


 



        #endregion



   }
}
